using System.Collections.Generic;

namespace OracleInternal.BinXml
{
	internal class ObxmlObjectPool<T> where T : new()
	{
		private const int INITIAL_POOL_SIZE = 1000;

		private Queue<T> m_ObjectPool;

		private int m_PoolSize = -1;

		private readonly object m_syncPool;

		internal bool IsEnabled => m_PoolSize != -1;

		public ObxmlObjectPool(int poolsize)
		{
			m_syncPool = new object();
			if (poolsize > 0)
			{
				m_PoolSize = poolsize;
			}
			else
			{
				m_PoolSize = ObxmlDecodeContext.m_XMLTypeNodePoolSize;
			}
			m_ObjectPool = new Queue<T>(1000);
		}

		internal T Get()
		{
			if (m_ObjectPool.Count > 0)
			{
				return m_ObjectPool.Dequeue();
			}
			return new T();
		}

		internal int Count()
		{
			return m_ObjectPool.Count;
		}

		internal void Put(T obj)
		{
			if (m_ObjectPool.Count < m_PoolSize)
			{
				m_ObjectPool.Enqueue(obj);
			}
		}

		internal void Purge(int size = 0)
		{
			if (m_ObjectPool != null && m_ObjectPool.Count > 0)
			{
				m_ObjectPool.Clear();
			}
		}
	}
}
